package com.iot.inter.controller.newinterface.trafficusage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.common.DeviceFlowUsage;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newrequest.trafficusage.appointdevicemonth.request.RequestAppointDeviceMonth;
import com.iot.inter.entity.newrequest.trafficusage.appointdevicemonth.response.ResponseAppointDeviceMonth;
import com.iot.inter.interfaceprocessing.newinterface.TurnTrafficUsageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

/**
 * 去北京做：3.5	查询指定设备指定月份的流量使用情况:2.0接口
 * <p>
 * 接入方可以通过调用该接口，可查询指定设备在指定月份的流量使用情况。
 *
 * @author lm
 * @date 2019/8/5
 */
@RestController
@Api(description = "3.5 查询指定设备指定月份的流量使用情况")
public class TrafficUsageAppointDeviceMonthController {

    private static Logger logger = Logger.getLogger(TrafficUsageAppointDeviceMonthController.class);

    @CrossOrigin
    @RequestMapping(value = "appointDeviceMonthTrafficUsage", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.5 查询指定设备指定月份的流量使用情况", response = ResponseAppointDeviceMonth.class)
    public String appointDeviceMonthTrafficUsage(@ApiParam @RequestBody RequestAppointDeviceMonth request) {

        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(request);

        if (requestStr != null) {

            logger.info("【查询指定设备指定月份的流量使用情况】请求：" + requestStr);

            RequestAppointDeviceMonth requestPackage = JSONArray.parseObject(requestStr, RequestAppointDeviceMonth.class);

            String month = requestPackage.getMonth();
            String deviceId = requestPackage.getDeviceId();
            String secret = requestPackage.getSecret();
            String partnerCode = requestPackage.getPartnerCode();


            TurnTrafficUsageService service = new TurnTrafficUsageService();
            DeviceFlowUsage response = service.appointDeviceMonthTrafficUsage(deviceId, month, partnerCode, secret);

            if (response != null) {
                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(response.getRetCode())) {

                    ResponseAppointDeviceMonth res = new ResponseAppointDeviceMonth();
                    res.setFlow(response.getFlow());
                    res.setRetCode(response.getRetCode());
                    res.setResultInfo(response.getResultInfo());
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(res);
                    logger.info("【查询指定设备指定月份的流量使用情况】成功响应：" + jsonString);
                    return jsonString;

                } else {
                    ResponseAppointDeviceMonth res = new ResponseAppointDeviceMonth();
                    res.setRetCode(response.getRetCode());
                    res.setResultInfo(response.getResultInfo());
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(res);
                    logger.info("【查询指定设备指定月份的流量使用情况】响应：" + jsonString);
                    return jsonString;
                }

            } else {
                ResponseAppointDeviceMonth res = new ResponseAppointDeviceMonth();
                res.setRetCode("9998");
                res.setResultInfo("响应为空");
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(res);
                logger.info("【查询指定设备指定月份的流量使用情况】响应：" + jsonString);
                return jsonString;
            }
        } else {
            ResponseAppointDeviceMonth response = new ResponseAppointDeviceMonth();
            response.setRetCode("9998");
            response.setResultInfo("请求为空");
            jsonString = JSON.toJSONString(response);
            logger.info("【查询指定设备指定月份的流量使用情况】响应：" + jsonString);
            return jsonString;
        }


    }
}